package com.donger.business.upms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donger.business.upms.entity.SysApp;
import com.donger.business.upms.mapper.SysAppMapper;
import com.donger.business.upms.service.ISysAppService;
import com.donger.common.core.exception.BizException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class SysAppServiceImpl extends ServiceImpl<SysAppMapper, SysApp> implements ISysAppService {


    /**
     * 设置默认
     * @param entity
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setAsDefault(SysApp entity) {
        SysApp currentApp = Optional.ofNullable(this.getById(entity.getId()))
                .orElseThrow(() -> new BizException("内容不存在"));
        this.update(Wrappers.<SysApp>lambdaUpdate()
                .set(SysApp::getActive,"N")
                .ne(SysApp::getId,entity.getId())
        );
        currentApp.setActive("Y");
        this.updateById(currentApp);
    }

    @Override
    public List<SysApp> getlistBytrue() {

        List<SysApp> list = this.list(Wrappers.<SysApp>lambdaQuery().eq(SysApp::getHidden, "1"));
        return list;
    }
}
